package com.bestcem.xm.award.controller.param.point;

import com.bestcem.xm.award.enums.AwardTypeEnum;
import com.bestcem.xm.award.service.bo.award.AwardAwardBO;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;

import static com.bestcem.xm.award.constant.Constants.DATE_PATTERN;

/**
 * @author xa.zhang <xa.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/8/17 14:36
 * @desc 积分奖励创建修改参数，除了id，其他全部必填
 */
@Data
public class PointAwardSaveParam {

    private String id;

    /**
     * 项目id
     */
    @NotBlank(message = "项目id必填")
    @JsonProperty("project_id")
    private String projectId;

    /**
     * 公司id
     */
    @JsonProperty("org_id")
    private String orgId;

    /**
     * 奖励名称
     */
    @NotBlank(message = "奖励名称必填")
    private String name;

    /**
     * 奖励基本配置
     */
    @Valid
    @NotNull(message = "缺少基本配置")
    private Basic basic;

    /**
     * 限制规则
     */
    @Valid
    @NotNull(message = "缺少限制规则")
    private Rule rule;

    /**
     * 积分奖励设置
     */
    @Valid
    @NotNull(message = "缺少积分奖励设置")
    @JsonProperty("band_point")
    private BandPoint bandPoint;

    /**
     * 限制规则
     */
    @Data
    public static class Rule implements Serializable {

        /**
         * 可选,答题时间限制
         */
        private Integer time;

        /**
         * 可选 账户访问限制
         */
        private Integer account;
    }

    /**
     * 奖励基本配置项
     */
    @NoArgsConstructor
    @AllArgsConstructor
    @Data
    public static class Basic implements Serializable {
        /**
         * 抽奖开始时间
         */
        @NotNull
        @JsonProperty("start_time")
        private String startTime;

        /**
         * 抽奖结束时间
         */
        @NotNull
        @JsonProperty("stop_time")
        private String stopTime;

        /**
         * 红包页面展示的公司名称
         */
        @NotBlank(message = "企业名称必填")
        @Length(max = 10, message = "企业名称长度最多10")
        @JsonProperty("display_name")
        private String displayName;

        /**
         * 红包页面展示的logo的id
         */
        @JsonProperty("display_logo_id")
        private String displayLogoId;

        /**
         * 红包页面展示的logo的的路径
         */
        @JsonProperty("display_logo_path")
        private String displayLogoPath;
    }

    /**
     * 品牌积分配置
     */
    @NoArgsConstructor
    @AllArgsConstructor
    @Data
    public static class BandPoint implements Serializable {

        /**
         * 单份奖励 1-1000
         */
        @NotNull(message = "单份奖励必填")
        @Min(value = 1, message = "单份奖励最小值1")
        @Max(value = 99999, message = "单份奖励最大值99999")
        @JsonProperty("sin_point")
        private Integer sinPoint;

        /**
         * 品牌积分id
         */
        @NotNull(message = "积分id必填")
        @JsonProperty("point_id")
        private String pointId;

        /**
         * 品牌积分序号
         */
        private String code;

        /**
         * 品牌积分名称
         */
        @NotBlank(message = "积分名称必填")
        @JsonProperty("point_name")
        private String pointName;

        /**
         * 来源备注
         */
        @Length(max = 10, message = "来源备注长度必须在0-10")
        @NotBlank(message = "来源备注必填")
        private String remark;

        /**
         * 可领取的积分次数，可为空，为空表示不限制
         */
        @Range(min = 1,max = 999999,message = "积分最大领取次数999999，最小1")
        private Integer total;

        /**
         * true表示有效期内达到领奖次数则提前结束奖励
         */
        @JsonProperty("stop_advance")
        private Boolean stopAdvance;

        /**
         * 领奖提示
         */
        @NotBlank(message = "领奖提示必填")
        @Length(max = 300, message = "领奖提示最多300字符")
        private String tip;
    }


    public static AwardAwardBO createAward(PointAwardSaveParam param) {

        AwardAwardBO award = new AwardAwardBO();

        award.setId(param.getId());
        award.setName(param.getName());
        award.setOrgId(param.getOrgId());
        award.setProjectId(param.getProjectId());
        award.setTType(AwardTypeEnum.POINT.getIndex());

        AwardAwardBO.Basic basic = new AwardAwardBO.Basic();
        if (param.getBasic() != null) {

            basic.setStartTime(DateUtil.getDate(cn.hutool.core.date.DateUtil.parse(param.getBasic().getStartTime(), DATE_PATTERN)));
            basic.setStopTime(DateUtil.getDate(cn.hutool.core.date.DateUtil.parse(param.getBasic().getStopTime(), DATE_PATTERN)));

            basic.setDisplayLogoId(param.getBasic().getDisplayLogoId());
            basic.setDisplayName(param.getBasic().getDisplayName());
            basic.setDisplayLogoPath(param.getBasic().getDisplayLogoPath());
        }


        AwardAwardBO.Rule rule = new AwardAwardBO.Rule();
        rule.setAccount(param.getRule().getAccount());
        rule.setTime(param.getRule().getTime());

        AwardAwardBO.BandPoint bandPoint = new AwardAwardBO.BandPoint();
        if (param.getBandPoint() != null) {
            BeanUtils.copyProperties(param.getBandPoint(), bandPoint);
        }

        award.setBasic(basic);
        award.setRule(rule);
        award.setBandPoint(bandPoint);

        return award;
    }

}
